package com.base.core.network;

import cn.hutool.http.HttpUtil;
import com.base.core.network.http.ResponseEntity;
import com.base.core.network.http.support.HttpHutool;
import com.base.core.network.http.support.HttpOkHttp3;
import com.base.core.exception.SimpleHttpException;
import com.base.core.network.http.Http;
import com.base.core.network.http.HttpHeader;
import com.base.core.network.http.support.HttpClient;
import com.base.core.utils.ClassUtils;

import java.util.Map;

/**
 * <p>
 * 请求工具类
 * </p>
 *
 * @author becker
 * @date Created in 2019/12/24 18:15
 */
public class SimpleHttpUtils {
    private static Http proxy;

    static {
        Http defaultProxy = null;
        ClassLoader classLoader = HttpUtil.class.getClassLoader();
        // 基于 java 11 HttpClient
        if (ClassUtils.isPresent("java.net.http.HttpClient", classLoader)) {
            defaultProxy = null;//待实现
        }
        // 基于 okhttp3
        else if (ClassUtils.isPresent("okhttp3.OkHttpClient", classLoader)) {
            defaultProxy = new HttpOkHttp3();
        }
        // 基于 httpclient
        else if (ClassUtils.isPresent("org.apache.http.impl.client.HttpClients", classLoader)) {
            defaultProxy = new HttpClient();
        }
        // 基于 hutool
        else if (ClassUtils.isPresent("cn.hutool.http.HttpRequest", classLoader)) {
            defaultProxy = new HttpHutool();
        }
        proxy = defaultProxy;
    }

    public static void setHttp(Http http) {
        proxy = http;
    }

    private static void checkHttpNotNull(Http proxy) {
        if (null == proxy) {
            throw new SimpleHttpException("HTTP 实现类未指定！");
        }
    }

    /**
     * GET 请求
     *
     * @param url URL
     * @return 结果
     */
    public static ResponseEntity get(String url) {
        checkHttpNotNull(proxy);
        return proxy.get(url);
    }

    /**
     * GET 请求
     *
     * @param url    URL
     * @param params 参数
     * @param encode 是否需要 url encode
     * @return 结果
     */
    public static ResponseEntity get(String url, Map<String, String> params, boolean encode) {
        checkHttpNotNull(proxy);
        return proxy.get(url, params, encode);
    }

    /**
     * GET 请求
     *
     * @param url    URL
     * @param params 参数
     * @param header 请求头
     * @param encode 是否需要 url encode
     * @return 结果
     */
    public static ResponseEntity get(String url, Map<String, String> params, HttpHeader header, boolean encode) {
        checkHttpNotNull(proxy);
        return proxy.get(url, params, header, encode);
    }

    /**
     * POST 请求
     *
     * @param url URL
     * @return 结果
     */
    public static ResponseEntity post(String url) {
        checkHttpNotNull(proxy);
        return proxy.post(url);
    }

    /**
     * POST 请求
     *
     * @param url  URL
     * @param data JSON 参数
     * @return 结果
     */
    public static ResponseEntity post(String url, String data) {
        checkHttpNotNull(proxy);
        return proxy.post(url, data);
    }

    /**
     * POST 请求
     *
     * @param url    URL
     * @param data   JSON 参数
     * @param header 请求头
     * @return 结果
     */
    public static ResponseEntity post(String url, String data, HttpHeader header) {
        checkHttpNotNull(proxy);
        return proxy.post(url, data, header);
    }

    /**
     * POST 请求
     *
     * @param url    URL
     * @param params form 参数
     * @param encode 是否需要 url encode
     * @return 结果
     */
    public static ResponseEntity post(String url, Map<String, String> params, boolean encode) {
        checkHttpNotNull(proxy);
        return proxy.post(url, params, encode);
    }

    /**
     * POST 请求
     *
     * @param url    URL
     * @param params form 参数
     * @param header 请求头
     * @param encode 是否需要 url encode
     * @return 结果
     */
    public static ResponseEntity post(String url, Map<String, String> params, HttpHeader header, boolean encode) {
        checkHttpNotNull(proxy);
        return proxy.post(url, params, header, encode);
    }
}
